package se.webbzon.boltzmann.game.object.portal;

import java.awt.Color;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.friendly.Friendly;

public class DoorPortalDeflation extends AbstractPortalDeflation {
	
	private DoorEnum doorType;
	
	private String textureSetName;
	
	private Color textureBlending = Color.white;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty door portal deflation. **/
	public DoorPortalDeflation(int deflationProperties) {
		super(deflationProperties);
	}
	
	/** Creates a new door portal deflation of an existing door portal. **/
	public DoorPortalDeflation(int deflationProperties, DoorPortal portal) {
		super(deflationProperties,portal);
		doorType = portal.getDoorType();
		textureSetName = portal.getTextureSetName();
		textureBlending = portal.getTextureBlending();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		// Read from buffer
		doorType = DoorEnum.values()[buff.getInt()];
		byte[] textureSetNameData = new byte[buff.getShort()];
		buff.get(textureSetNameData, 0, textureSetNameData.length);
		final int red, green, blue, alpha;
		red = 0x000000FF & buff.get();
		green = 0x000000FF & buff.get();
		blue = 0x000000FF & buff.get();
		alpha = 0x000000FF & buff.get();
		textureBlending = new Color(red,green,blue,alpha);
		
		// Decode strings
		try {
			textureSetName = new String(textureSetNameData,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			textureSetName = "";
		}
	}
	
	@Override public ByteBuffer deflate(int allocate) {
		// Encode strings
		byte[] textureSetNameData;
		try {
			textureSetNameData = textureSetName.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			textureSetNameData = new byte[0];
		}
		
		ByteBuffer buff = super.deflate(allocate + textureSetNameData.length + 10);
		
		// Write to buffer
		buff.putInt(doorType.ordinal());
		buff.putShort((short) textureSetNameData.length);
		buff.put(textureSetNameData);
		buff.put((byte) textureBlending.getRed());
		buff.put((byte) textureBlending.getGreen());
		buff.put((byte) textureBlending.getBlue());
		buff.put((byte) textureBlending.getAlpha());
		
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new road portal from the road portal deflation. **/
	@Friendly DoorPortal makePortal(AbstractEnvironment env) {
		DoorPortal portal = new DoorPortal(	env, getWorldAreaName(), 
											getPortalId(), doorType, 
											textureSetName,textureBlending);
		
		portal.setPosition(getX(), getY(), getZ());
		portal.setSize(getWidth(), getHeight(), getDepth());
		portal.setRotation(getEX(), getEY(), getEZ(), getAngle());
		
		return portal;
	}

}
